<?php
abstract class oauth_api_abstract implements oauth_api_interface {

	protected $_session = null;
	protected $_errors = array();
	protected $_name = 'twitter';	
	protected $adapter = null;
	protected $cookiejar = null;
	
	public function getError() {
		return $this->_errors;
	}

	/**
	 * @return oauth_api_abstract | oauth_api_interface
	 */
	public function addError($msg, $code = null) {
		if($code === null) {
			$this->_errors[] = $msg;
		} else {
			$this->_errors[$code] = $msg;
		}
		return $this;
	}

	/**
	 * @return oauth_api_abstract | oauth_api_interface
	 */
	public function setCallback($callback) {
		if(!$callback) {
			$callback = $this->getRedirectUrl(false);
		}
		$this->getSession()->setCallback($callback);
		return $this;
	}
	
	public function getCallback() {
		return $this->getSession()->getCallback();
	}
	
	/**
	 * @return oauth_api_token
	 */
	public function getToken() {
		return new oauth_api_token($this->getSession()->getToken());
	}
	
	/**
	 * @return oauth_api_session
	 */
	public function getSession() {
		if($this->_session === null) {
			$this->_session = new oauth_api_session();
		}
		return $this->_session;
	}
	
	/**
	 * @return system_setting_oauth
	 */
	public function getSource() {
		return $this->_source;
	}
	
	public function getRedirectUrl($sq = true) {
		$front = system_controller_front::getInstance();
		$request = $front->getRequest();
		$port = $request->getServer('SERVER_PORT');
		$port = $port == 80 ? '' : ':' . $port;
		$url = $request->getScheme() . '://' . $request->getServer('HTTP_HOST') . $port .
				$front->getRouter()->assemble($request->getControllerName() . '/' . $request->getActionName(), $request->getParams());
		if($sq && $query = $request->getQuery()) {
			return $url . '?' . http_build_query($query);
		} else {
			return $url;
		}
	}
	
	public function get($url, $returnHeader = false) {
		curl_setopt($this->getAdapter(), CURLOPT_URL, $url);
		curl_setopt($this->getAdapter(), CURLOPT_POST, false);
		$this->setCookieJar();
		curl_setopt($this->getAdapter(), CURLOPT_HEADER, $returnHeader);
		return curl_exec($this->getAdapter());
	}
	
	public function post($url, $parameters = array(), $returnHeader = false) {
		curl_setopt($this->getAdapter(), CURLOPT_URL, $url);
		curl_setopt($this->getAdapter(), CURLOPT_POST, true);
		$this->setCookieJar();
		curl_setopt($this->getAdapter(), CURLOPT_POSTFIELDS, $parameters);
		curl_setopt($this->getAdapter(), CURLOPT_HEADER, $returnHeader);
		return curl_exec($this->getAdapter());
	}
	
	protected function setAdapter($adapter = null) {
		if(null == $adapter) {
			$this->adapter = curl_init();
			curl_setopt($this->adapter, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($this->adapter, CURLOPT_SSL_VERIFYPEER, false);
		} else {
			$this->adapter = $adapter;
		}
	}
	
	protected function getAdapter() {
		return $this->adapter;
	}
	
	protected function setCookieJar() {
		if($this->cookiejar == null) {
			$this->cookiejar = tempnam(_DIRECTORY_VAR . '/tmp', 'OAUTH');
		}
		curl_setopt($this->getAdapter(), CURLOPT_COOKIEFILE, $this->cookiejar);
		curl_setopt($this->getAdapter(), CURLOPT_COOKIEJAR, $this->cookiejar);
	}

	/**
	 * @return oauth_api_response
	 */
	protected function getOauthResponse() {
		$returnClass = 'oauth_response_' . $this->_name;
		$response = new $returnClass($this->getSource());
		foreach($this->getError() AS $code => $msg) {
			$response->addError($msg, $code);
		}
		return $response;
	}
	
	/**
	 * @return system_controller_request_http
	 */
	protected function getRequest() {
		return system_controller_front::getInstance()->getRequest();
	}
	
	protected $_source = null;
	public function __construct() {
		$this->setAdapter();
		$this->_source = new system_setting_oauth($this->_name);
	}
	
	public function __destruct() {
		@unlink($this->cookiejar);
	}
	
}
